package com.jamal2362.apktool.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;

public class RefreshListView extends ListView {
	private RefreshListener listener;
	private float touchY;
	private float distanceY;
	private float refreshDistance;

	public RefreshListView(Context context) {
		super(context);
		init(context);
	}

	public RefreshListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public RefreshListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
		//refreshDistance = 300;
		refreshDistance = context.getApplicationContext().getResources().getDisplayMetrics().heightPixels / 4.5f;
	}

	public void setDistance(float distance) {
		this.refreshDistance = distance;
	}

	public float getDistance() {
		return this.refreshDistance;
	}

	@Override
	public boolean dispatchTouchEvent(final MotionEvent ev) {
		if (scrolly() > 0) {
			distanceY = 0;
			if (listener != null) {
				listener.update(distanceY, refreshDistance);
			}
			return super.dispatchTouchEvent(ev);
		}
		switch (ev.getAction()) {
			case MotionEvent.ACTION_DOWN:
				touchY = ev.getY();
				distanceY = 0;
				if (listener != null)
					listener.update(0, refreshDistance);
				break;

			case MotionEvent.ACTION_MOVE:
				distanceY = ev.getY() - touchY;
				if (listener != null) {
					postDelayed(new Runnable()
						{
							@Override
							public void run() {
								if (scrolly() == 0)
									listener.update(distanceY, refreshDistance);
								else
									touchY = ev.getY();
							}
						}, 300);
				}
				break;

			case MotionEvent.ACTION_UP:
				final boolean refresh = distanceY >= refreshDistance;
				distanceY = 0;
				if (listener != null) {
					if (refresh) {
						listener.onRefresh();
					}
					postDelayed(new Runnable()
						{
							@Override
							public void run() {
								listener.update(distanceY, refreshDistance);
							}
						}, 300);
				}
				break;
		}
		return super.dispatchTouchEvent(ev);
	}

	public int scrolly() {
		if (getChildCount() > 0) {
			View c = getChildAt(0);
			return -c.getTop() + getFirstVisiblePosition() * c.getHeight();
		} else {
			return 0;
		}
	}

	public interface RefreshListener {
		public void onRefresh();
		public void update(float distance, float refreshDistance);
	}

	public void setRefreshListener(RefreshListener listener) {
		this.listener = listener;
	}

	public Drawable rotateDrawable(final Drawable d, final float angle) {
		final Drawable[] arD = {d};
		return new LayerDrawable(arD) {
			@Override
			public void draw(final Canvas canvas) {
				canvas.save();
				canvas.rotate(-(float)(angle * Math.PI / 180), d.getBounds().width() / 2, d.getBounds().height() / 2);
				super.draw(canvas);
				canvas.restore();
			}
		};
	}
}
